var systemAllMenuList = [{
    "icon": "&#xe61e;",
    "title": "智能生产",
    "subMenus": [{
      "title": "产能查看",
      "url": "queryCapacity.html"
    }, {
      "title": "员工达成率看板",
      "url": "reachKanBan.html"
    }, {
      "title": "车间尾数看板",
      "url": "mantissa.html"
    }, {
      "title": "车间拖期看板",
      "url": "delay.html"
    }, {
      "title": "周转量看板",
      "url": "turnover.html"
    }, {
      "title": "车间遗留看板",
      "url": "kanBan/leftOver.html"
    }, {
      "title": "产品质量分析改进看板",
      "url": "makeBetter.html"
    }, {
      "title": "订单查询",
      "url": "intelligentProduction/querryOrder/index.html"
    }]
  }, {
    "icon": "&#xe6ba;",
    "title": "智能电气",
    "subMenus": [{
      "title": "设备信息",
      "url": "powerWorkBench/equipmentInfo/index.html"
    }, {
      "title": "全局监控",
      "url": "powerWorkBench/globalMonitor/index.html"
    }, {
      "title": "电气监控",
      "url": "powerWorkBench/electricalMonitor/index.html"
    }, {
      "title": "设备稼动率",
      "url": "powerWorkBench/machineDynamicRate/index.html"
    }, {
      "title": "宿舍水电管理",
      "url": "powerWorkBench/dormMgr/index.html"
    }, {
      "title": "仪表管理",
      "url": "powerWorkBench/energyMgr/index.html"
    }, {
      "title": "仪表用量汇总",
      "url": "powerWorkBench/energyDosageStat/index.html"
    }, {
      "title": "空气流量控制",
      "url": "powerWorkBench/masterSwitch/index.html"
    }, {
      "title": "智能看板",
      "url": "datav/index.html",
      nonAuthority: true
    }, {
      "title": "物联网卡设置",
      "url": "remoteEquipment/cardSetting/index.html",
      nonAuthority: true,
      checkValid: function (user) {
        var companyId = user.companyId;
        return companyId === 28;
      }
    }]
  }, {
    "icon": "&#xe687;",
    "title": "生产订单",
    "subMenus": [{
      "title": "生产订单制单",
      "prefix": "./",
      "url": "html/createOrder/index_x.html"
    }, {
      "title": "生产订单列表",
      "url": "orderlist.html"
    }, {
      "title": "设备订单列表",
      "url": "productOrderMgr/deviceOrder/index.html"
    }, {
      "title": "生产订单流程卡制单",
      "prefix": "./",
      "url": "html/createOrder/indexcard_x.html"
    }, {
      "title": "模版列表",
      "jumpUrl": "bomBusiness/orderTemplate/index.html",
      "url": "ordertemplatelist.html"
    }, {
      "title": "业务订单列表",
      "url": "orderywdlist.html",
      "prefix": "./",
      "hidden": true,
      "nonAuthority": true
    }, {
      "title": "业务订单模版制作",
      "url": "html/createOrder/index_x_ywd.html",
      "prefix": "./",
      "hidden": true,
      "nonAuthority": true
    }, {
      "title": "业务订单制单",
      "url": "html/createOrder/index_x_ywd_list.html",
      "prefix": "./",
      "hidden": true,
      "nonAuthority": true
    }, {
      "title": "订单编辑",
      "url": "html/createOrder/orderEdit.html",
      "prefix": "./",
      "hidden": true,
      "nonAuthority": true
    }, {
      "title": "订单审核",
      "url": "html/createOrder/orderAudit.html",
      "prefix": "./",
      "hidden": true,
      "nonAuthority": true
    }, {
      "title": "订单查看",
      "url": "html/createOrder/orderSee.html",
      "prefix": "./",
      "hidden": true,
      "nonAuthority": true
    }, {
      "title": "订单修改申请",
      "url": "html/createOrder/orderApplyMainForApply.html",
      "prefix": "./",
      "hidden": true,
      "nonAuthority": true
    }, {
      "title": "订单申请修改",
      "url": "html/createOrder/orderApplyMainForSeeAndAudit.html",
      "prefix": "./",
      "hidden": true,
      "nonAuthority": true
    }, {
      "title": "生产订单模版",
      "url": "./bomBusiness/produceOrder/template.html",
      "hidden": true,
      "nonAuthority": true
    }, {
      "title": "生产订单",
      "url": "./bomBusiness/produceOrder/createOrder.html",
      "hidden": true,
      "nonAuthority": true
    }, {
      "title": "生产订单申请修改",
      "url": "./bomBusiness/produceOrder/applyOrder.html",
      "hidden": true,
      "nonAuthority": true
    }, {
      "title": "公司物料类型表",
      "showTitle": "物料类型表",
      "url": "purchaseOrder/companyMaterialType/index.html"
    }]
  }, {
    "icon": "&#xe627;",
    "title": "订单管理",
    "subMenus": [{
      "title": "商务订单",
      "url": "bomBusiness/businessOrder/index.html"
    }]
  }, {
    "icon": "&#xe673;",
    "title": "采购管理",
    "subMenus": [{
      "title": "投料单",
      "url": "purchaseOrder/feddingBill/index.html"
    }, {
      "title": "补料采购",
      "url": "purchaseOrder/createOrder/index.html"
    }, {
      "title": "库存采购",
      "url": "purchaseOrder/createWarehouse/index.html"
    }, {
      "title": "查询采购单",
      "url": "purchaseOrder/queryPurchaseOrder/index.html"
    }, {
      "title": "查询预采购单",
      "url": "purchaseOrder/queryPrePurchaseOrder/index.html"
    }, {
      "title": "供应商表",
      "showTitle": "供应商表",
      "url": "purchaseOrder/customer/provider/index.html",
    }, {
      "title": "客户表",
      "showTitle": "客户表",
      "url": "purchaseOrder/customer/client/index.html",
    }, {
      "title": "公司物料表",
      "showTitle": "物料表",
      "url": "purchaseOrder/companyMaterialTable/index.html"
    }, {
      "title": "公司现货表",
      "showTitle": "现货表",
      "url": "purchaseOrder/spotMaterial/index.html"
    }, {
      "title": "物料显示配置表",
      "url": "purchaseOrder/materialSetting/index.html"
    }, {
      "title": "物料申补单",
      "url": "purchaseOrder/materialSupplyOrder/index.html",
      nonAuthority: true,
      checkValid: function (user) {
        return MenuUtil.hasPageValid([
          "/applyRepairForm.html",
          "/queryApplyRepairForm.html"
        ], user);
      }
    }, {
      "title": "订单备份信息",
      "url": "purchaseOrder/duplicateOrderUpdateRecord/index.html"
    }]
  }, {
    "icon": "&#xe643;",
    "title": "仓库管理",
    "subMenus": [{
      "title": "库存查询",
      "url": "warehouse/materialStock/index.html"
    }, {
      "title": "出入库查询",
      "url": "warehouse/warehouseInOutDetail/index.html"
    }, {
      "title": "物料入库",
      "url": "warehouse/purchaseWarehouseIn/index.html"
    }, {
      "title": "出库给客户",
      "url": "finishProduceOut.html"
    }, {
      "title": "车间领料单",
      "url": "warehouse/warehouseOutFeed/index.html"
    }, {
      "title": "生成发料单",
      "url": "warehouse/createIssueNote/index.html"
    }, {
      "title": "查询发料单",
      "url": "warehouse/queryIssueNote/index.html"
    }, {
      "title": "退货给供应商",
      "url": "warehouse/returnWarehouseToProvider/index.html"
    }, {
      "title": "返还给车间",
      "url": "warehouse/returnWarehouseToWorkShop/index.html"
    }, {
      "title": "库存盘点",
      "url": "warehouse/stockTaking/index.html"
    }, {
      "title": "车间多余物料",
      "url": "warehouse/surplusMaterial/index.html"
    }, {
      "title": "物料转仓",
      "url": "warehouse/materialTransfer/index.html"
    }, {
      "title": "车间统计",
      "url": "warehouse/workshopStat/index.html"
    }]
  }, {
    "icon": "&#xe625",
    "title": "成品管理",
    "subMenus": [{
      "title": "成品库存查询",
      "showTitle": "库存查询",
      "url": "productWarehouseMgr/queryWarehouse/index.html"
    }, {
      "title": "成品出入库查询",
      "showTitle": "出入库查询",
      "url": "productWarehouseMgr/queryInoutRecord/index.html"
    }, {
      "title": "成品销售",
      "url": "productWarehouseMgr/productSale/index.html"
    }, {
      "title": "库存盘点",
      "url": "productWarehouseMgr/stockTaking/index.html"
    }, {
      "title": "成品转仓",
      "url": "productWarehouseMgr/materialTransfer/index.html",
      nonAuthority: true
    }]
  },
  {
    "icon": "&#xe66a",
    "title": "商城管理",
    "subMenus": [{
      "title": "上下架", //物料管理
      "url": "shoppingMallMgr/materialMgr/index.html"
    }, {
      "title": "商城", //已经发布的物料进行收藏
      "url": "shoppingMallMgr/releasedMaterial/index.html"
    }, {
      "title": "导入物料记录",
      "url": "shoppingMallMgr/importMaterialRecord/index.html"
    }, {
      "title": "订购单",
      "url": "shoppingMallMgr/purchaseMaterialOrder/index.html"
    }, {
      "title": "商家信息",
      "url": "shoppingMallMgr/businessManInfo/index.html"
    }]
  },
  {
    "icon": "&#xe63a;",
    "title": "财务管理",
    "subMenus": [{
      "title": "车间原料统计",
      "url": "rowMaterialCount.html",
      "hidden": false
    }, {
      "title": "仓库物料统计",
      "url": "financeMgr/materialStockMoney/index.html"
    }, {
      "title": "应付账款",
      "url": "financeMgr/warehouseBalance/index.html"
    }, {
      "title": "应收账款",
      "url": "financeMgr/outBalance/index.html"
    }, {
      "title": "收付款记录",
      "url": "financeMgr/receiptPaymentMode/index.html"
    }, {
      "title": "出入库财务汇总",
      "url": "financeMgr/warehouseInOutMoney/index.html"
    }, {
      "title": "订单成本汇总",
      "url": "financeMgr/orderCostGlasses/index.html"
    }, {
      "title": "财务审批设置",
      "url": "attendApprovalProcessAccount.html"
    }, {
      "title": "报销审批记录",
      "url": "financeMgr/reimbursementApprovalRecord/index.html"
    }, {
      "title": "罚款审批记录",
      "url": "financeMgr/punishmentyApprovalRecord/index.html"
    }, {
      "title": "奖励审批记录",
      "url": "financeMgr/rewardApprovalRecord/index.html"
    }, {
      "title": "财务报表",
      "url": "financeMgr/financialStatements/index.html",
      checkValid: function (user) {
        var companyId = user.companyId;
        return companyId === 28;
      }
    }, {
      "title": "银行卡设置",
      "url": "financeMgr/bankCardSetting/index.html"
    }, {
      "title": "单据中心",
      "url": "productMgr/historyOrderMgr/index.html"
    }, {
      "title": "预收付款",
      "url": "financeMgr/advancePayment/index.html"
    }]
  }, {
    "icon": "&#xe639;",
    "title": "外协管理",
    "subMenus": [{
      "title": "出入库记录",
      "url": "outBusinessMgr/warehouse/index.html"
    }, {
      "title": "外协供应商表",
      "url": "outBusinessMgr/supplier/index.html"
    }, {
      "title": "外协周期表",
      "url": "outBusinessMgr/prodCycle/index.html"
    }, {
      "title": "外协单列表",
      "url": "outBusinessMgr/orderTable/index.html"
    }, {
      "title": "外协返回列表",
      "url": "outBusinessMgr/returnOrderTable/index.html"
    }, {
      "title": "外协流转",
      "url": "outBusinessMgr/orderCirculation/index.html",
      "nonAuthority": true,
      "isPayment": true
    }]
  }, {
    "icon": "&#xe63c;",
    "title": "生产管理",
    "subMenus": [{
      "title": "工序表",
      "url": "processTable.jsp",
      // "jumpUrl":"productMgr/workProcessTable/index.html"
    }, {
      "title": "生产周期表",
      "url": "workshopProductionCycle.jsp",
      "isPayment": true
    }, {
      "title": "设置车间审核人员",
      "url": "setWorkShopApprovalReportPeople.html"
    }, {
      "title": "我的报工",
      "url": "myApproval.html",
      nonAuthority: true
    }, {
      "title": "审核报工",
      "url": "approval.html"
    }, {
      "title": "订单短出设置",
      "url": "duanChu.jsp"
    }, {
      "title": "报工",
      "url": "report.html"
    }]
  }, {
    "icon": "&#xe637;",
    "title": "IQC",
    "subMenus": [{
      "title": "物料检验类型",
      "url": "iqc/materialCheckType/index.html",
      nonAuthority: true
    }, {
      "title": "来料检验",
      "url": "iqc/iqcReport/index.html",
      nonAuthority: true
    }, {
      "title": "来料检验审核",
      "url": "iqc/iqcReportApproval/index.html",
      nonAuthority: true
    }]
  }, {
    "icon": "&#xe6e1;",
    "title": "质检管理",
    "subMenus": [{
      "title": "我的qc质检",
      "url": "productMgr/myQcReport/index.html",
      nonAuthority: true
    }, {
      "title": "qc质检审核",
      "url": "productMgr/qcReportApproval/index.html"
    }, {
      "title": "异常单查询",
      "url": "productMgr/exceptionTable/index.html"
    }]
  }, {
    "icon": "&#xe62e;",
    "title": "工程管理",
    "subMenus": [{
      "title": "设置工序模板",
      "showTitle": "工序模板设置",
      "url": "orderProcedureTemplate.html"
    }, {
      "title": "设置用户工序",
      "showTitle": "用户工序设置",
      "url": "userProcedure.html"
    }, {
      "title": "订单实际报工工序",
      "showTitle": "订单工序设置",
      "url": "setOrderProcedure.html"
    }, {
      "title": "工序待生产",
      "url": "engineering/surplusQuantity/index.html"
    }, {
      "title": "物料配置表",
      "url": "treeLeftToRight.html"
    }, {
      "title": "工艺表",
      "url": "MaterialTechnology/index.html"
    }, {
      "title": "配件工艺设置",
      "url": "partsProcedure/partsProcedure.html"
    }, {
      "title": "物料组合工艺设置",
      "url": "partsProcedure/manyPartsProcedure.html"
    }, {
      "title": "订单配件工艺",
      "url": "partsProcedure/orderProcedure.html"
    }, {
      "title": "工程参数设置",
      "url": "projectSet.html"
    }, {
      "title": "订单属性设置",
      "url": "engineering/orderPropertySetting/index.html"
    }, {
      "title": "损耗配置表",
      "url": "bomBusiness/materialLossSetting/index.html"
    }, {
      "title": "难易系数配置",
      "url": "bomBusiness/facilityLevelSetting/index.html"
    }, {
      "title": "层级配置表",
      "url": "bomBusiness/materialClassify/index.html"
    }, {
      "title": "bom清单",
      "url": "bomBusiness/bomMaterialSetting/index.html"
    }]
  }, {
    "icon": "&#xe6d0;",
    "title": "售后管理",
    checkFun: function (user) {
      var type = _.get(user, 'companyInfo.type');
      return type === 2;
    },
    "subMenus": [{
      "title": "设备维修管理",
      "url": "workorderlist.html"
    }, {
      "title": "设备维修列表",
      "url": "workorderapplylist.html?board=false"
    }, {
      "title": "设备维修报价",
      "url": "machineRepair/repairQuote/index.html"
    }, {
      "title": "设备维修等级配置",
      "url": "machineRepair/repairSetting/index.html"
    }, {
      "title": "维修操作手册",
      "url": "machineRepair/repairRecord/index.html"
    }, {
      "title": "警报设置",
      "url": "machineRepair/warnSetting/index.html",
      nonAuthority: true
    }]
  }, {
    "icon": "&#xe62c;",
    "title": "人事行政",
    "subMenus": [{
      "title": "考勤机管理",
      "url": "attendDeviceManage.html",
    }, {
      "title": "考勤审批流程设置",
      "url": "attendApprovalProcess.html"
    }, {
      "title": "考勤组管理",
      "url": "attendanceGroup.html"
    }, {
      "title": "班次管理",
      "url": "attendanceClasses.html"
    }, {
      "title": "加班规则",
      "url": "attendOvertimeRule.html",
    }, {
      "title": "奖惩制度设定",
      "url": "attendanceRewardAndPunish.html"
    }, {
      "title": "考勤月度汇总",
      "url": "attendanceMgr/monthSummary/index.html"
    }, {
      "title": "每日统计",
      "url": "attendanceMgr/perDayStat/index.html"
    }, {
      "title": "打卡时间",
      "url": "attendanceMgr/attendanceRecord/index.html"
    }, {
      "title": "原始记录",
      "url": "attendanceMgr/attendanceSourceRecord/index.html"
    }, {
      "title": "请假审批记录",
      "url": "attendanceMgr/takeOffApprovalRecord/index.html"
    }, {
      "title": "补卡审批记录",
      "url": "attendanceMgr/supplementCardApprovalRecord/index.html"
    }, {
      "title": "加班审批记录",
      "url": "attendanceMgr/overtimeApprovalRecord/index.html"
    }, {
      "title": "添加人员",
      "url": "addPeople.html"
    }, {
      "title": "部门表",
      "url": "departmentTable.html"
    }, {
      "title": "车间表",
      "url": "workshopProductionCycle.jsp",
      "isPayment": false
    }, {
      "title": "职员表",
      "url": "personnelList.html"
    }, {
      "title": "职员工资明细",
      "url": "personnelMgr/salaryDetail/index.html"
    }]
  }, {
    "icon": "&#xe61d;",
    "title": "系统设置",
    "subMenus": [{
      "title": "审批设置",
      "url": "setApproval.html"
    }, {
      "title": "权限管理",
      "url": "roleManager.html"
    }, {
      "title": "用户权限管理",
      "url": "userAndRole.html"
    }, {
      "title": "公司权限管理",
      "url": "systemSetting/companyAuthoritySetting/index.html",
      checkValid: function (user) {
        var companyId = user.companyId;
        return companyId === 157 || companyId === 28;
      },
      nonAuthority: true
    }, {
      "title": "公司设定",
      "url": "companyManager.html"
    }, {
      "title": "公司邮箱设定",
      "url": "SystemSetup/index.html"
    }, {
      "title": "微信异常提醒设置",
      "url": "addMessageSinkUser.html"
    }, {
      "title": "公司通用模版设置",
      "url": "systemSetting/templateSetting/index.html"
    }, {
      "title": "付费版试用",
      "url": "systemSetting/testPaidVersion/index.html",
      "nonAuthority": true,
      "isPayment": false
    }]
  }
];

var MenuUtil = {
  authorityMenus: [],
  getAuthorityMenus: function (permissionList, user) {
    permissionList = _.map(permissionList, function (permission) {
      return permission.substring(1);
    });

    var menus = [];
    var isPayUser = user.companyInfo.status === 1;
    for (var i = 0; i < systemAllMenuList.length; i++) {
      var menu = systemAllMenuList[i];
      var menuCheckFun = menu.checkFun;
      if (menuCheckFun !== undefined) {
        if (!menuCheckFun(user)) {
          continue;
        }
      }

      menu = _.cloneDeep(menu);
      var subMenus = menu.subMenus;
      subMenus = _.filter(subMenus, function (obj) {
        var isPayment = obj.isPayment;
        if (isPayment !== undefined) {
          if (isPayment) {
            if (!isPayUser) {
              return false;
            }
          } else {
            if (isPayUser) {
              return false;
            }
          }
        }
        var checkFun = obj.checkValid;
        if (checkFun !== undefined) {
          if (!checkFun(user)) {
            return false;
          }
        }
        if (obj.nonAuthority) {
          return true;
        }
        var url = obj.url;
        var index = url.indexOf('?');
        if (index !== -1) {
          url = url.substring(0, index);
        }
        return permissionList.indexOf(url) !== -1;
      });
      if (subMenus.length > 0) {
        var hiddenMenus = _.filter(subMenus, {
          hidden: true
        });
        if (hiddenMenus.length === subMenus.length) {
          continue;
        }
        menu.subMenus = subMenus;
        menus.push(menu);
      }
    }

    this.authorityMenus = menus;
    return menus;
  },
  hasPageValid: function (checkPages, user) {
    var pageList = user.pageList;
    for (var i = 0; i < checkPages.length; i++) {
      var checkPage = checkPages[i];
      if (pageList.indexOf(checkPage) === -1) {
        return false;
      }
    }
    return true;
  },

  getMenuUrls: function () {
    var menuUrls = this.menuUrls;
    if (!SysUtil.isEmpty(menuUrls)) {
      return menuUrls;
    }

    menuUrls = [];
    for (var i = 0; i < systemAllMenuList.length; i++) {
      var menu = systemAllMenuList[i];
      var subMenus = menu.subMenus;
      for (var j = 0; j < subMenus.length; j++) {
        var subMenu = subMenus[j];
        var url = subMenu.url;
        if (url.indexOf('/') !== 0) {
          url = '/' + url;
        }
        menuUrls.push(url);
      }
    }
    this.menuUrls = menuUrls;
    return menuUrls;
  },
  orderMenus: function (permissionList) {
    var menuUrls = this.getMenuUrls();
    permissionList = _.orderBy(permissionList, [function (obj) {
      var url = obj.url;
      var index = menuUrls.indexOf(url);
      if (url === -1) {
        return Infinity;
      }
      return index;
    }], ['asc']);
    return permissionList;
  }
};
